package routers

import (
	"github.com/gin-gonic/gin"
	"modbus-go-demo/global"
	"net/http"
	"path"
)

func Cors() gin.HandlerFunc {
	// 注意使用此跨域 router 配置请求方法必须加上OPTIONS(可以直接用Any) , 只有POST的话会报错 404
	return func(context *gin.Context) {
		context.Header("Access-Control-Allow-Origin", "*")
		context.Header("Access-Control-Allow-Headers", "Content-Type,AccessToken,X-CSRF-Token, Authorization, Token, x-token")
		context.Header("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE, PATCH, PUT")
		context.Header("Access-Control-Expose-Headers", "Content-Length, Access-Control-Allow-Origin, Access-Control-Allow-Headers, Content-Type")
		context.Header("Access-Control-Allow-Credentials", "true")
		if context.Request.Method == "OPTIONS" {
			//context.AbortWithStatus(http.StatusNoContent)
			context.AbortWithStatus(http.StatusOK)
		}
	}

}

func GinConfigure(r *gin.Engine) {
	// 静态文件
	r.Static("/static", global.StaticPath)
	r.Static("/upload", path.Join(global.StaticPath, "upload"))
	r.Static("/tmp", path.Join(global.StaticPath, "tmp"))
	//r.StaticFile("/MP_verify_6zPTYlHxnIHuVKAF.txt", "./static/MP_verify_6zPTYlHxnIHuVKAF.txt")
	r.StaticFile("/favicon.ico", path.Join(global.StaticPath, "favicon.ico"))
	r.StaticFile("/robots.txt", path.Join(global.StaticPath, "robots.txt"))
	//r.Use(Cors())

}
